﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace WindowsFormsApplication1.CrystalReport
{
    public static class crystalReportConnectDB
    {
        // 数据库连接配置
        public struct connectionDB
        {
            public string ip;
            public string port;
            public string database;
            public string userId;
            public string pw;
        };

        public static connectionDB connectDB;

        public static void updateCrystalReport(CrystalDecisions.CrystalReports.Engine.Tables tables)
        {
            try
            {
                foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
                {
                    CrystalDecisions.Shared.TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
                    tableLogOnInfo.ConnectionInfo.ServerName = crystalReportConnectDB.connectDB.ip + "," + crystalReportConnectDB.connectDB.port;
                    tableLogOnInfo.ConnectionInfo.DatabaseName = crystalReportConnectDB.connectDB.database;
                    tableLogOnInfo.ConnectionInfo.UserID = crystalReportConnectDB.connectDB.userId;
                    tableLogOnInfo.ConnectionInfo.Password = crystalReportConnectDB.connectDB.pw;

                    table.ApplyLogOnInfo(tableLogOnInfo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        public static bool configXMLdata()
        {
            return readfile();
        }

        private static void parsestring(string s)
        {
            int len = s.Length;
            int index = s.IndexOf(';');
            string temp = s.Substring(12, index - 12);
 

            int index1 = s.IndexOf(',');
            temp = s.Substring(12, index1 - 12);
            crystalReportConnectDB.connectDB.ip = temp;

            temp = s.Substring(index1 + 1, index - index1 - 1);
            crystalReportConnectDB.connectDB.port = temp;


            string s1 = s.Substring(index + 1, len - index - 1);
            len = s1.Length;

            index = s1.IndexOf(';');
            temp = s1.Substring(16, index - 16);

            crystalReportConnectDB.connectDB.database = temp;


            s1 = s1.Substring(index + 1, len - index - 1);

            len = s1.Length;

            index = s1.IndexOf(';');
            temp = s1.Substring(8, index - 8);

            crystalReportConnectDB.connectDB.userId = temp;


            s1 = s1.Substring(index + 1, len - index - 1);

            len = s1.Length;

            index = s1.IndexOf(';');
            temp = s1.Substring(9, index - 9);

            crystalReportConnectDB.connectDB.pw = temp;

        }

        private static bool readfile()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("Monitor.exe.config");
                XmlNodeList nodeList = doc.SelectNodes("/configuration/connectionStrings/add");
                string s = nodeList[0].Attributes[1].Value;
                parsestring(s);

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

        }
    }
}
